<script setup>
import VIcon from "../../../components/icon/VIcon.vue";
import avatar from '../../../assets/images/avatar.jpg';
import {ElLoading, ElMessageBox} from "element-plus";
import store from "../../../store/index.js";
import router from "../../../router/index.js";

// 处理用户注销登录
const handleLogout = () => {
  ElMessageBox.confirm(
      '是否退出当前登录账号?',
      '退出登录',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    const loadingInstance = ElLoading.service({
      fullscreen: true,
      lock: true,
      background: 'rgba(0, 0, 0, 0.7)',
      text: '正在注销...'
    })
    // 注销登录
    store.dispatch('login/handleLogout');
    let timer = setTimeout(() => {
      loadingInstance.close();
      clearTimeout(timer);
      // 跳转到登录页面
      router.push({name: 'login'});
    }, 500);
  });
};
</script>

<template>
  <div class="v-profile-container">
    <img :src="avatar" alt="头像" style="width: 34px;height: 34px;border-radius: 50%;">
    <div class="v-profile-dropdown">
      <div class="v-profile-avatar">
        <img :src="avatar" alt="头像" style="width: 44px;height: 44px;border-radius: 50%;">
        <span>Mr.小朱同学</span>
      </div>
      <el-divider border-style="dashed" style="margin: 14px 0;"/>
      <ul class="v-profile-list">
        <li>
          <v-icon icon="Setting" size="17"/>
          <span>个人中心</span>
        </li>
        <li>
          <v-icon icon="Reading" size="17"/>
          <span>Vue3笔记</span>
        </li>
        <li @click.stop="handleLogout">
          <v-icon icon="SwitchButton" size="17"/>
          <span>退出登录</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<style scoped lang="scss">
.v-profile-container {
  margin-left: 10px;
  cursor: pointer;
  position: relative;
  
  .v-profile-dropdown {
    display: none;
    z-index: 100000;
    position: absolute;
    right: 0;
    top: 100%;
    width: 180px;
    padding: 14px;
    background-color: white;
    border: 1px solid #ccc;
    border-radius: 8px;
    box-sizing: border-box;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    opacity: 0;
    transition: all 0.5s ease-in-out;
    
    .v-profile-avatar {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      
      > span {
        margin-left: 10px;
        color: #252f4a;
      }
    }
    
    .v-profile-list {
      list-style: none;
      
      > li {
        font-size: 14px;
        margin-bottom: 10px;
        cursor: pointer;
        display: flex;
        align-items: center;
      }
      
      > li:hover {
        background-color: #F1F1F4;
        border-radius: 4px;
      }
    }
  }
}

.v-profile-container:hover > .v-profile-dropdown {
  display: inline-block;
  opacity: 1;
}
</style>